package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.WarningInfoMapper;
import com.example.demo.entity.Message;
import com.example.demo.entity.WarningInfos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class GetWarningByIdController {
    @Autowired
    private WarningInfoMapper wm;
    @RequestMapping("/getwarningbyid")
    public void getWarningById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int warningid;
        try {
             warningid=Integer.valueOf(request.getParameter("warningid"));
        } catch (NumberFormatException e) {
            response.getWriter().write(JSONObject.toJSONString(Message.failure("format error")));
            return;
        }
        WarningInfos w=wm.selectByWarningId(warningid);
        if(w!=null){
            response.getWriter().write(JSONObject.toJSONString(Message.success(w)));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Can not find this warning")));
    }
}
